<?php
namespace Tiger\Api\Utils;

use App\Transformers\ApiError;
use Endroid\QrCode\QrCode;
use Illuminate\Support\Facades\Log;

class Helper
{



    static public function  QrCode($info,$logo=null)
    {

        $qrCode = new QrCode();

        $filename = md5(microtime()) . '.' . strtolower($qrCode->getImageType());
       // $realPrefix='../storage';
        $storageRelativeDir = '/app/code/'.date('Ymd').'/';


        if (!is_dir(storage_path($storageRelativeDir))) {
            mkdir(storage_path() . $storageRelativeDir, 0777, true);
        }
        chmod(storage_path() .$storageRelativeDir, 0777);
        if(!is_writable(storage_path().$storageRelativeDir)) {
            ApiError::errorForbidden('文件夹没有写权限');
        }

        $realFileName=storage_path().$storageRelativeDir.$filename;


        if($logo)
        {
            $qrCode->setText(json_encode($info))
                ->setSize(300)
                ->setPadding(10)
                ->setLogo($logo)
                ->setErrorCorrection('high')
                ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
                ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
                ->setLabelFontSize(16)
                ->setImageType(QrCode::IMAGE_TYPE_PNG)
                ->save($realFileName);
        }
        else
        {
            $qrCode->setText(json_encode($info))
                ->setSize(300)
                ->setPadding(10)
                ->setErrorCorrection('high')
                ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
                ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
                ->setLabelFontSize(16)
                ->setImageType(QrCode::IMAGE_TYPE_PNG)
                ->save($realFileName);
        }
       return  $url=url('storage'.$storageRelativeDir.$filename);
    }



}